﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Gmis.Public;

namespace Gmis.Web.Manage.Party
{
    /// <summary>
    /// PMember 的摘要说明
    /// </summary>
    public class PMemberChangeHandler : IHttpHandler
    { 
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string sId = context.Request["id"];
            string action = context.Request["action"];          
            //
            if (string.IsNullOrEmpty(action))
            {
                context.Response.Write(CommonFunction.failure("无指定操作"));
            }
            else
            {
                switch (action.ToLower())
                {
                    case "add":
                        context.Response.Write(add());
                        break;
                    case "delete":
                        context.Response.Write(delete(sId));
                        break;
                    case "deleteall":
                        context.Response.Write(deleteAll(context.Request["ids"]));
                        break;
                    case "edit":
                        context.Response.Write(update());
                        break;
                }
            }
        }
        private Model.PMemberChange GenerateModel()
        {
            //获取form数据
            Model.PMemberChange model = new Model.PMemberChange();
            model.sId = CommonFunction.getRequestValue("txt_sId");
            if (model.sId == "") model.sId = CommonFunction.GetGuid();//主键
            model.sMemberId = CommonFunction.getRequestValue("lookup.sId");
            model.sName = CommonFunction.getRequestValue("txt_sName");//姓名 context.Request["txt_sName"];     
            model.sNumber = CommonFunction.getRequestValue("txt_sNumber");//编号
            model.sSex = CommonFunction.getRequestValue("cbx_sSex");//性别
            model.sIdentity = CommonFunction.getRequestValue("txt_sIdentity");//身份证号        
            model.sMarry = CommonFunction.getRequestValue("cbx_sMarry");//婚姻状况
            model.sNation = CommonFunction.getRequestValue("cbx_sNation");//民族
            model.sEducation = CommonFunction.getRequestValue("cbx_sEducation");//学历
            model.sWork = CommonFunction.getRequestValue("txt_sWork");//职业
            model.sWorkPlace = CommonFunction.getRequestValue("txt_sWorkPlace");//工作单位
            model.sMobile = CommonFunction.getRequestValue("txt_sMobile");//手机号码
            model.sComment = CommonFunction.getRequestValue("txt_sComment");//备注
            return model;
        }
        //增加一条
        private string add()
        {
            BLL.PMemberChange bll = new BLL.PMemberChange();
            if (bll.Add(GenerateModel()))
            {
                return CommonFunction.successAndCloseCurrent("", "", "PMemberChange");
            }
            else
            {
                return CommonFunction.failure();
            }
        }
        //删除一条
        private string delete(string sId)
        {
            BLL.PMemberChange bll = new BLL.PMemberChange();
            if (bll.Delete(sId))
            {
                return CommonFunction.success();
            }
            else
            {
                return CommonFunction.failure();
            }
        }
        //删除全部
        private string deleteAll(string ids)
        {
            string[] strs = ids.Split(',');
            try
            {
                BLL.PMemberChange bll = new BLL.PMemberChange();
                foreach (string str in strs)
                {
                    bll.Delete(str);
                }
                return CommonFunction.success();
            }
            catch { return CommonFunction.failure(); }


        }
        //更新数据
        private string update()
        {
            BLL.PMemberChange bll = new BLL.PMemberChange();
            if (bll.Update(GenerateModel()))
            {
                return CommonFunction.successAndCloseCurrent("", "", "PMemberChange");
            }
            else
            {
                return CommonFunction.failure();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}